package process

import (
	"fmt"
	"net"
	"os"
	"testgin0012/chat/common"
	"time"
)

type UserProcess struct {
}

func NewUserProcess() *UserProcess {
	return &UserProcess{}
}

func (receiver *UserProcess) SayOneStart(conn *net.Conn, UserId int, ToUserId int) {
	fmt.Printf("用户%v发给用户%v的消息:", UserId, ToUserId)
	var content string
	fmt.Scanln(&content)

	subMessage := common.RequestSayOneStart{
		FromUserId: UserId,
		ToUserId:   ToUserId,
		Content:    content,
	}
	common.NewConnManager(*conn).RequestStruct(subMessage, common.TypeRequestSayOneStart)
}
func (receiver *UserProcess) Test(conn *net.Conn, UserId int) {
	//var Content string
	//fmt.Print("测试数据为: ")
	//fmt.Scanf("%s\n",&Content)
	//
	//subMessage := common.RequestTest{
	//	Content:Content,
	//}
	//common.NewConnManager(*conn).RequestStruct(subMessage, common.TypeRequestTest)
}
func (receiver *UserProcess) SayOne(conn *net.Conn, UserId int, ClientBlocked *bool) {
	if UserId <= 0 {
		fmt.Println("登录后才能看用户列表")
		fmt.Println()
		return
	}
	var SheUserId int
	fmt.Print("请输入要私聊的用户id: ")
	fmt.Scanf("%d\n", &SheUserId)

	subMessage := common.RequestSayOne{
		UserId:    UserId,
		SheUserId: SheUserId,
	}
	common.NewConnManager(*conn).RequestStruct(subMessage, common.TypeRequestSayOne)
	for {
		if *ClientBlocked {
			receiver.SayOneStart(conn, UserId, SheUserId)
		} else {
			time.Sleep(time.Second)
		}
	}
}
func (receiver *UserProcess) SayAll(conn *net.Conn, UserId int) {
	if UserId <= 0 {
		fmt.Println("登录后才能看用户列表")
		fmt.Println()
		return
	}
	var Content string
	fmt.Print("请输入要说的内容: ")
	fmt.Scanln(&Content)

	subMessage := common.RequestSayAll{
		UserId:  UserId,
		Content: Content,
	}
	common.NewConnManager(*conn).RequestStruct(subMessage, common.TypeRequestSayAll)
}
func (receiver *UserProcess) List(conn *net.Conn, UserId int) {
	if UserId <= 0 {
		fmt.Println("登录后才能看用户列表")
		fmt.Println()
		return
	}
	subMessage := common.RequestUserList{
		UserId: UserId,
	}
	common.NewConnManager(*conn).RequestStruct(subMessage, common.TypeRequestUserList)
}

func (receiver *UserProcess) Login(conn *net.Conn, user *common.User) {
	if user.UserId > 0 {
		fmt.Println("用户已登录")
		return
	}
	var UserId int
	var PassWord string
	fmt.Print("请输入用户编号: ")
	fmt.Scanf("%d\n", &UserId)
	fmt.Print("请输入用户密码: ")
	fmt.Scanf("%s\n", &PassWord)

	subMessage := common.RequestLogin{
		UserId:   UserId,
		PassWord: PassWord,
	}
	common.NewConnManager(*conn).RequestStruct(subMessage, common.TypeRequestLogin)
}
func (receiver *UserProcess) Exit(conn *net.Conn) {
	(*conn).Close()
	os.Exit(0)
}
func (receiver *UserProcess) Register(conn *net.Conn) {
	var UserId int
	var NickName string
	var PassWord string
	fmt.Print("请输入用户编号: ")
	fmt.Scanf("%d\n", &UserId)
	fmt.Print("请输入用户密码: ")
	fmt.Scanf("%s\n", &PassWord)
	fmt.Print("请输入用户昵称: ")
	fmt.Scanf("%s\n", &NickName)

	subMessage := common.RequestRegister{
		UserId:   UserId,
		PassWord: PassWord,
		NickName: NickName,
	}
	common.NewConnManager(*conn).RequestStruct(subMessage, common.TypeRequestRegister)
}
